package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
	@Autowired
	private EmployeeService employeeService;
	@PostMapping("/login")
	public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
		//1.将页面提交的密码password进行md5加密，得到加密的字符串
		String password = employee.getPassword();
		password = DigestUtils.md5DigestAsHex(password.getBytes());
		//2.判断用户名是否存在
		LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();//添加查询条件的
		lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
		Employee employee1 = employeeService.getOne(lambdaQueryWrapper);
		//3.用户不存在，登录失败
		if (employee1==null){
			return R.error("用户不存在，登录失败");
		}
		//4.用户存在,比较密码，如果密码不对，登录失败
		if (!password.equals(employee1.getPassword())){
			return R.error("登录失败");
		}
		//5.用户被禁用，登录失败
		if (employee1.getStatus()==0){
			return R.error("登录失败");
		}
		//6.都成功则登录成功
		request.getSession().setAttribute("employee", employee1.getId());
		return R.success(employee1);
	}
	/**
	 * 员工退出
	 * @param
	 * @return
	 */
	@PostMapping("/logout")
	public R logout(HttpSession session){
		//清理Session中保存的当前登录员工的id
		session.invalidate();
		//request.getSession().removeAttribute("employee");
		return R.success("退出成功");
	}
	@PostMapping
	public R<String> save(HttpSession session,@RequestBody Employee employee){
		log.info("新增员工，员工信息：{}",employee.toString());
		//密码进行md5加密
		employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
		// employee.setCreateTime(LocalDateTime.now());
		// employee.setUpdateTime(LocalDateTime.now());

		//获取当前登录用户的id
		// Long empID = (Long) session.getAttribute("employee");
		// employee.setCreateUser(empID);
		// employee.setUpdateUser(empID);

		employeeService.save(employee);
		return R.success("添加员工成功");
	}

	@GetMapping("/page")
	public R<Page> page(Integer page,Integer pageSize,String name){
		// A. 构造分页条件
		Page pageInfo = new Page(page,pageSize);
		// B. 构建搜索条件 - name进行模糊匹配
		LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
		// C. 构建排序条件 - 更新时间倒序排序
		queryWrapper.orderByDesc(Employee::getUpdateTime);
		// D. 执行查询
		employeeService.page(pageInfo,queryWrapper);
		// E. 组装结果并返回
		return R.success(pageInfo);
	}

	/**
	 * 启用禁用功能
	 * 路径：/employee
	 * 参数：json参数
	 * 请求方法：put
	 */
	@PutMapping
	public R<String> update(@RequestBody Employee employee,HttpSession session){
		log.info("启用禁用。。");
		if(employee.getId()==1){
			return R.error("管理员不能禁用");
		}
		// employee.setUpdateTime(LocalDateTime.now());
		// employee.setUpdateUser((long) session.getAttribute("employee"));
		//修改状态
		// employee.setStatus(employee.getStatus()==0?1:0);
		employeeService.updateById(employee);
		return R.success("启用禁用成功");
	}
	/**
	 * 根据id查询员工信息
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public R<Employee> getById(@PathVariable Long id){
		log.info("根据id查询员工信息...");
		Employee employee = employeeService.getById(id);
			return R.success(employee);
	}

}
